package com.kebabeaters.nakebab.client.place;

import java.util.List;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.gwtplatform.dispatch.shared.DispatchAsync;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;
import com.kebabeaters.nakebab.client.presenter.ContentPresenter;
import com.kebabeaters.nakebab.shared.action.tasterranking.GetTasterRankingAction;
import com.kebabeaters.nakebab.shared.action.tasterranking.TasterRankingEntry;
import com.kebabeaters.nakebab.shared.dto.samples.UserReviewsDummy;
import com.kebabeaters.nakebab.shared.dto.structures.Mark;
import com.kebabeaters.nakebab.shared.dto.structures.Restaurant;
import com.kebabeaters.nakebab.shared.dto.structures.Review;

public class RestaurantDetailsPresenter
		extends
		Presenter<RestaurantDetailsPresenter.MyView, RestaurantDetailsPresenter.MyProxy> {

	@SuppressWarnings("unused")
	private final PlaceManager placeManager;
	private final DispatchAsync dispatcher;

	public interface MyView extends View {
		void setName(String name);

		void setAddress(String adress);

		void setPrice(String price);

		void setSauce(String sauce);

		void setHours(String shift);

		void setAdditional(String add);

		void setRev1(Mark mark);

		void setRev2(Mark mark);

		void setUserReviews(List<Review> reviews);
	}

	@ProxyCodeSplit
	@NameToken(NameTokens.restaurantDetails)
	public interface MyProxy extends ProxyPlace<RestaurantDetailsPresenter> {
	}

	@Inject
	public RestaurantDetailsPresenter(final EventBus eventBus,
			final MyView view, final MyProxy proxy, PlaceManager placeManager,
			DispatchAsync dispatcher) {
		super(eventBus, view, proxy);
		this.placeManager = placeManager;
		this.dispatcher = dispatcher;
	}

	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(this, ContentPresenter.TYPE_Content, this);
	}

	@Override
	protected void onBind() {
		super.onBind();
	}

	@Override
	protected void onReveal() {
		super.onReveal();
	}

	@Override
	public void prepareFromRequest(PlaceRequest request) {
		super.prepareFromRequest(request);

		final String restaurantName = request.getParameter(ParamTokens.id,
				"Doner");

		dispatcher.execute(new GetTasterRankingAction(),
				new AsyncCallback<GetTasterRankingAction.Result>() {

					private List<TasterRankingEntry> resultList;
					private Restaurant restaurant;
					private List<Mark> marks;

					@Override
					public void onFailure(Throwable caught) {
						caught.printStackTrace();
					}

					@Override
					public void onSuccess(GetTasterRankingAction.Result result) {
						resultList = result.getEntries();

						for (TasterRankingEntry entry : resultList) {
							if (restaurantName.equalsIgnoreCase(entry
									.getRestaurant().getName())) {
								restaurant = entry.getRestaurant();
								marks = entry.getBestTastersMarks();
							}
						}

						getView().setName(restaurant.getName());
						getView().setAddress("Poznań, ul. Fredry 13");
						getView().setPrice("8 zl - 13 zl");
						getView()
								.setSauce(
										"ostry, ziołowy, czosnkowy, keczup, musztarda");
						getView().setHours("14 - 2");
						getView().setAdditional("inne dania: pizza, spaghetti");
						getView().setRev1(marks.get(0));
						getView().setRev2(marks.get(1));
					}
				});

		getView().setUserReviews(UserReviewsDummy.getList());
	}
}
